package com.tedu.udm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.udm.ex.ServiceException;
import com.tedu.udm.mapper.UserMapper;
import com.tedu.udm.mapper.UserTeamMapper;
import com.tedu.udm.pojo.entity.User;
import com.tedu.udm.pojo.entity.UserTeam;
import com.tedu.udm.service.IUserTeamService;
import com.tedu.udm.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;

@Service
@Slf4j
public class UserTeamServiceImpl implements IUserTeamService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserTeamMapper userTeamMapper;
    @Override
    public void addNew(Long teamId, Long[] userIds) {
        log.debug("开始处理批量添加小组成员业务");

        Collection<UserTeam> userTeams = new ArrayList<>();
        for (Long userId : userIds) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("id",userId);
            User user = userMapper.selectById(userId);
            if (user == null) {
                continue;
            }
            UserTeam userTeam = new UserTeam();
            userTeam.setUserId(userId);
            userTeam.setTeamId(teamId);
            userTeams.add(userTeam);
        }
        int rows = userTeamMapper.insertBatchSomeColumn(userTeams);
        if (rows<1){
            String message = "服务器忙，请稍后再试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }
}
